<template>
	<view class="work-container">
		<!-- 轮播图 -->
		<!-- <uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
      <swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
        <swiper-item v-for="(item, index) in data" :key="index">
          <view class="swiper-item" @click="clickBannerItem(item)">
            <image :src="item.image" mode="aspectFill" :draggable="false" />
          </view>
        </swiper-item>
      </swiper>
    </uni-swiper-dot> -->
		<view class="current-location">
			<uni-icons type="location" size="20"></uni-icons>
			<text>{{ housingName }}</text>
		</view>
		<!-- 宫格组件 -->
		<uni-section title="工作台" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false" @change="changeGrid">
				<uni-grid-item v-for="(item,index) in funcList" :key="`ff${index}`" :index="index">
					<view class="grid-item-box">
						<uni-icons :type="item.icon" size="30"></uni-icons>
						<text class="text">{{item.text}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
		
	</view>
</template>

<script>
	import { getDeviceDetailWithCode } from '@/api/device/device'
	export default {
		data() {
			return {
				current: 0,
				swiperDotIndex: 0,
				data: [{
						image: '/static/images/banner/banner01.jpg'
					},
					{
						image: '/static/images/banner/banner02.jpg'
					},
					{
						image: '/static/images/banner/banner03.jpg'
					}
				],
				funcList:[
					{
						id:3,
						icon:'location',
						text:'选择小区'
					},
					{
						id:1,
						icon:'scan',
						text:'设备扫码'
					},
					{
						id:2,
						icon:'search',
						text:'设备code查询'
					}
				]
			}
		},
		computed:{
			housingName(){
				const housing = this.$store.getters.housing
				return housing?housing.housingName:'暂未设置小区'
			}
		},
		
		methods: {
			clickBannerItem(item) {
				console.info(item)
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			changeGrid(e) {
				const item = this.funcList[e.detail.index]
				switch (item.id){
					case 1: {
						if(!this.$store.getters.housing){
							this.$modal.msgError('请先选择小区')
							return
						}
						this.$tab.navigateTo(`/pages/work/scan-code/index`)
						break;
					}
					case 2: {
						this.showCodeSearchModal()
						break;
					}
					case 3: {
						this.selectTheHousing()
						break
					}
					default:break;
				}
			},
			// 设备code查询弹窗
			showCodeSearchModal(){
				if(!this.$store.getters.housing){
					this.$modal.msgError('请先选择小区')
					return
				}
				
				uni.showModal({
					title:'请输入设备code',
					editable:true,
					placeholderText:'设备code',
					success: (res) => {
						if (res.confirm){
							this.searchDeviceWithCode(res.content)
						}
					}
				})
			},
			// code查询设备
			searchDeviceWithCode(code){
				if (!code){
					return
				}
				this.$tab.navigateTo(`/pages/work/device-detail/index?code=${code}`);
			},
			// 设置当前小区
			selectTheHousing(){
				this.$tab.navigateTo(`/pages/work/housing-list/index`)
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */
	
	.current-location {
		display: flex;
		align-items: center;
		padding: 10rpx 20rpx;
	}

	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
		;
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}
	.scanCode {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 99;
		height: 100%;
		width: 100%;
		background-color: rgba(0, 0, 0, 0.7);
	}
</style>